﻿using System;
using System.Globalization;
using Nop.Core;
using Nop.Services.Localization;

namespace Nop.Services.Helpers
{
    public static class Extensions
    {
        /// <summary>
        /// To string without seconds
        /// </summary>
        /// <param name="datetime"></param>
        /// <param name="workContext"></param>
        /// <param name="localizationService"></param>
        /// <returns></returns>
        public static string ToNoSecondString(this DateTime? datetime, IWorkContext workContext, ILocalizationService localizationService)
        {
            string na = localizationService == null ? "N/A" : localizationService.GetResource("Common.NotAvailable");
            return datetime.HasValue ? datetime.Value.ToNoSecondString(workContext) : na;
        }

        /// <summary>
        /// To string without seconds
        /// </summary>
        /// <param name="datetime"></param>
        /// <param name="workContext"></param>
        /// <returns></returns>
        public static string ToNoSecondString(this DateTime datetime, IWorkContext workContext)
        {
            if (workContext.WorkingLanguage == null)
                return "";

            CultureInfo culture = new CultureInfo(workContext.WorkingLanguage.LanguageCulture);
            return datetime.ToString("g", culture);
        }
    }
}


